package com.nursinghome.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.nursinghome.admin.mapper.ElderlyMapper;
import com.nursinghome.admin.mapper.MedicalStaffMapper;
import com.nursinghome.admin.mapper.UserMapper;
import com.nursinghome.admin.service.MedicalStaffService;
import com.nursinghome.bean.Elderly;
import com.nursinghome.bean.MedicalService;
import com.nursinghome.bean.MedicalStaff;
import com.nursinghome.bean.User;
import com.nursinghome.common.mapper.MedicalServiceMapper;
import com.nursinghome.exception.NurisingHomeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MedicalStaffServiceImpl implements MedicalStaffService {
    @Autowired
    private MedicalStaffMapper medicalStaffMapper;
    @Autowired
    private ElderlyMapper elderlyMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MedicalServiceMapper medicalServiceMapper;
    //查询所有护工
    @Override
    public List<MedicalStaff> selectAll(String mname, String mphone, String musername) {
        return medicalStaffMapper.selectAll(mname,mphone,musername);
    }
    //新增护工
    /**
     * 在护工表中新建创建一个护工
     * 在user表中查询这个员工是否存在如果存在就抛出异常
     * 在user表中创建一个账号默认密码123123
     * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addMedical(MedicalStaff medicalStaff) throws Exception {
        //判断该工号是否存在
        MedicalStaff byMno = medicalStaffMapper.selectByMno(medicalStaff.getMno());
        if (byMno != null){
            throw new NurisingHomeException("该工号已经存在");
        }
        //
        medicalStaffMapper.addMedical(medicalStaff);
        //user表中查询这个username是否存在如果存在就抛出异常
        User user = userMapper.selectByUsername(medicalStaff.getMusername());
        if (user != null){
            throw new NurisingHomeException("该用户名已经存在");
        }
        //在账号表里增加一条数据默认密码就是用户名
        User user1 = new User(null, medicalStaff.getMusername(), medicalStaff.getMusername(), 2);
        userMapper.insertUser(user1);
        return true;
    }
    //修改护工
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateMedical(MedicalStaff medicalStaff) throws NurisingHomeException {
        //判断护工是否存在
        MedicalStaff byMno = medicalStaffMapper.selectByMno(medicalStaff.getMno());
        if (byMno == null){
            throw new NurisingHomeException("该工号不存在");
        }
        // 判断是否需要修改护工的服务
        if (ObjectUtil.isNotEmpty(medicalStaff.getServiceIds())) {
            medicalServiceMapper.delete(medicalStaff.getMid());
            medicalStaff.getServiceIds().forEach(serviceId -> {
                MedicalService medicalService = new MedicalService() {{
                    setMedicalId(medicalStaff.getMid());
                    setServiceId(serviceId);
                }};
                //添加护工的服务
                medicalServiceMapper.insert(medicalService);
            });
        }
        return medicalStaffMapper.updateMedical(medicalStaff) == 1;
    }

    @Override
    public Boolean deleteMedical(Integer mid) throws NurisingHomeException {
        //查询护工是否还有需要照顾的老人如果有就不能删除
        List<Elderly> elderlies = elderlyMapper.selectByMidAndElderly(mid);
        if (elderlies.size() > 0){
            throw new NurisingHomeException("该员工还有老人无法删除");
        }
        return medicalStaffMapper.deleteMedical(mid) == 1;
    }

    @Override
    public MedicalStaff selectByMid(Integer mid) {
        return medicalStaffMapper.selectByMid(mid);
    }

    @Override
    public MedicalStaff selectByMno(String mno) {
        return medicalStaffMapper.selectByMno(mno);
    }

    @Override
    public Integer selectByMusername(String musername) {
        return medicalStaffMapper.selectByMusername(musername);
    }

    @Override
    public List<MedicalStaff> selectByEid(Long eid, Integer serviceId) {
        return medicalStaffMapper.selectByEid(eid , serviceId);
    }
}
